package opn.srn.command;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import opn.srn.event.IEvent;

public abstract class Command implements ICommand<Map<String, String>> {

	List<ICommand<?>> outHandlers = new ArrayList<ICommand<?>>();

	String name;

	public Command(String name) {
		this.name = name;
	}

	@Override
	public CmdSupportType getSupportedType() {
		return null;
	}

	@Override
	public void execute() throws Exception {
	}

	@Override
	public void onEvent(ICommand<?> source, IEvent event) throws Exception {
	}

	@Override
	public void fireEvent(IEvent event) throws Exception {
		fireEvent(this, event);
	}

	@Override
	public void fireEvent(ICommand<?> command, IEvent event) throws Exception {
		for (ICommand<?> outHandler : outHandlers) {
			outHandler.onEvent(command, event);
		}
	}

	@Override
	public List<String> getPossibleOutEvents() {
		return null;
	}

	@Override
	public List<String> getCanProcessInEvents() {
		return null;
	}

	@Override
	public void addOutHandler(ICommand<?> outHandler) {
		outHandlers.add(outHandler);
	}

	@Override
	public List<ICommand<?>> getOutHandlers() {
		return outHandlers;
	}

	@Override
	public void printOutHandlers(int tabIndex) {
		System.out.println(getTabSpaces(tabIndex) + name);
		for (ICommand<?> command : outHandlers) {
			((Command) command).printOutHandlers(tabIndex + 1);
		}
	}

	private String getTabSpaces(int tabIndex) {
		String retVal = "";
		for (int index = 0; index < tabIndex; index++) {
			retVal += "\t";
		}
		return retVal;
	}

}
